home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9646 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  75 lines

  1. Path: faw.uni-ulm.de!peinel
  2. From: peinel@faw.uni-ulm.de (Gertraud Peinel)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: malloc question
  5. Date: 12 Mar 1996 15:24:36 GMT
  6. Organization: FAW Ulm
  7. Distribution: world
  8. Message-ID: <4i44vk$g16@rigel.rz.uni-ulm.de>
  9. References: <4htonk$350@news.hklink.net> <danpop.826488975@rscernix>
  10. NNTP-Posting-Host: merlin.faw.uni-ulm.de
  11.  
  12. In article <danpop.826488975@rscernix>, danpop@mail.cern.ch (Dan Pop) writes:
  13. |> In <4htonk$350@news.hklink.net> alex@station.net (Alex Chu) writes:
  14. |> 
  15. |> >typedef struct item {
  16. |> >  int val;
  17. |> >  struct item *next;
  18. |> >} ITEM, *PITEM;
  19. |> >
  20. |> >main()
  21. |> >{
  22. |> >  PITEM head, current;
  23. |> >  head=(PITEM) malloc(sizeof(ITEM));
  24. |> >            ^^^^^^^
  25. |> >  head->val=1;
  26. |> >}
  27. |> >
  28. |> >I want to know why need to use the type casting PITEM in front of the
  29. |> >malloc ?  Please help!
  30. |> 
  31. |> You DON'T have to cast the value returned by malloc, casting it is actually
  32. |> a BAD idea.  But you DO have to include <stdlib.h> in any source file
  33. |> which calls malloc.
  34.  
  35. Hmmm, and what should I do with this (especially when I have to use these
  36. compilers) ? :
  37.  
  38.   sun5:/users/peinel/clang(36)> uname -a
  39.   SunOS sun5 4.1.3 3 sun4c
  40.   sun5:/users/peinel/clang(35)> cat oh.c 
  41.   #include <stdlib.h>
  42.  
  43.   typedef struct item {
  44.     int val;
  45.     struct item *next;
  46.   } ITEM, *PITEM;
  47.  
  48.   main()
  49.   {
  50.     PITEM head, current;
  51.     head= malloc(sizeof(ITEM));
  52.     head->val=1;
  53.   }
  54.   sun5:/users/peinel/clang(29)> cc oh.c
  55.   "oh.c", line 11: warning: illegal pointer combination
  56.   sun5:/users/peinel/clang(34)> which cc
  57.   /bin/cc
  58.   sun5:/users/peinel/clang(32)> CC oh.c
  59.   CC  mist.c:
  60.   "oh.c", line 11: error: no standard conversion of  char * to  struct item *
  61.   "oh.c", line 10: warning:  current not used
  62.   1 error
  63.   sun5:/users/peinel/clang(33)> which CC
  64.   /com/owc++/bin/CC
  65.  
  66. Without casting ?
  67.  
  68. `gp'
  69.  
  70. -- 
  71. gertraud peinel         / ____)   | |  ulm  /
  72. peinel@faw.uni-ulm.de  / /     /  | |      /
  73.                       / ___)  /__ | | / | /
  74.                     _/_/    _/   _| _/ __/
  75.